package apps.AfterDlcs.afterDlcsBL.buff.myBuff.Base;

import apps.AfterDlcs.AfterDlcs;
import apps.AfterDlcs.afterDlcsBL.Event;
import apps.AfterDlcs.afterDlcsBL.buff.Buff;
import apps.AfterDlcs.afterDlcsBL.buff.BuffKeys;
import backs.dataBase.DBNormal;

public class BaseBuffBuff extends Buff
{
    @Override
    public void handleEvent(Event event)
    {
        if(event.key.equals(BuffKeys.TIME_PASS)&&event.eventTurn==event.maxTurn)
        {
            for(Integer level:this.owner.buffMap.keySet())
            {
                for(Buff buff:this.owner.buffMap.get(level))
                {
                    if(buff.lastTime>=0)
                    {
                        buff.lastTime--;
                        if(buff.lastTime<=0)
                        {
                            buff.lastTime=Long.parseLong("0");
                            this.owner.warSand.handleEvent(new Event(owner,owner,owner,BuffKeys.BUFF_TIME_OVER,new Object[]{buff},6));
                        }

                    }
                }
            }
        }

        if(event.key.equals(BuffKeys.ADD_BUFF)&&event.eventTurn==event.maxTurn-2)
        {
            Buff buff=(Buff) event.values[0];
            if(buff.owner==this.owner)
            {
                buff.saveBuff();
                this.owner.buffMap.get(buff.level).add(buff);
            }
        }

        if(event.key.equals(BuffKeys.BUFF_TIME_OVER)&&event.eventTurn==event.maxTurn-2)
        {
            Buff buff=(Buff) event.values[0];
            if(buff.owner==this.owner)
            {
                this.owner.warSand.handleEvent(new Event(owner,owner,owner,BuffKeys.REMOVE_BUFF,new Object[]{buff},6));
            }

        }

        if(event.key.equals(BuffKeys.REMOVE_BUFF)&&event.eventTurn==event.maxTurn-2)
        {
            Buff buff=(Buff) event.values[0];
            if(buff.owner==this.owner)
            {
                DBNormal.delValue(AfterDlcs.dbName,worldID,owner.heroID+"_buffList_level_"+buff.level,buff.buffID+"");
                this.owner.buffMap.get(buff.level).remove(buff);
            }
        }
    }
}
